package com.penghaisoft.microwms.producthand.web.controller.intowarehouse;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProHandInDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.wms.model.ProHandIn;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProHandInService;

/**
 * @Description 成品手工入库
 * @author luoteng
 * @date 2018年6月14日 上午9:42:17 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/pro/handin/intowh")
public class HandInWhController extends BaseController {

	@Autowired
	private IProHandInService proHandInService;
	
	@Autowired
	private IBaseDictionaryService baseDictionaryService;
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;//注入对象
	
	/** 
	 * @Title confirm
	 * @Description 提单
	 * @author luoteng
	 * @date 2018年6月14日:上午10:15:19
	 * @param proHandInDto
	 * @return HandResp
	 */
	@PostMapping("loadBill")
	public HandResp confirm(@RequestBody ProHandInDto proHandInDto) {
		HandResp result = new HandResp();
		Resp resp = proHandInService.loadBill(proHandInDto.getHandInNo());
		result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
	}
	
	/** 
	 * @Title info
	 * @Description 详情
	 * @author luoteng
	 * @date 2018年6月14日:上午10:55:29
	 * @param proHandInDto
	 * @return HandResp
	 */
	@PostMapping("info")
	public HandResp info(@RequestBody ProHandInDto proHandInDto) {
		HandResp result = new HandResp();
		
		ProHandIn proHandIn = new ProHandIn();
		proHandIn.setHandInNo(proHandInDto.getHandInNo());
		proHandIn.setActiveFlag("1");
		List<ProHandIn> list = proHandInService.queryListByAny(proHandIn);
		
		BaseDictionary baseDictionary1 = new BaseDictionary();
    	baseDictionary1.setActiveFlag("1");
    	baseDictionary1.setDicType(Constant.MODEL_TYPE_P);
    	List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);

    	if(list != null && !list.isEmpty()){
    		for(ProHandIn tmpData : list){
    			if(list1 != null && !list1.isEmpty()){
    				for(BaseDictionary tmp : list1){
    					if(tmp.getDicCode().equals(tmpData.getModelType())){
    						tmpData.setModelTypeCn(tmp.getDicName());
    					}
    				}
    			}
    		}
    	}
    	
		result.setCode(Constant.SUCCESS);
		result.setList(list);
		return result;
	}
	
	/** 
	 * @Title scan
	 * @Description 扫码
	 * @author luoteng
	 * @date 2018年6月14日:上午11:08:57
	 * @param proHandInDto
	 * @return HandResp
	 */
	@PostMapping("scan")
	public HandResp scan(@RequestBody ProHandInDto proHandInDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_HANDIN_PREFIX + proHandInDto.getBarcode();
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = new TransResult();
			try{
				result = proHandInService.handInScanBarcode(proHandInDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}
		
		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
	
	/** 
	 * @Title del
	 * @Description 删除条码
	 * @author luoteng
	 * @date 2018年6月14日:下午5:10:45
	 * @param proHandInDto
	 * @return HandResp
	 */
	@PostMapping("del")
	public HandResp del(@RequestBody ProHandInDto proHandInDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_HANDIN_PREFIX + proHandInDto.getBarcode();
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = new TransResult();
			try{
				result = proHandInService.handInDelBarcode(proHandInDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}
		
		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
	
	/** 
	 * @Title complete
	 * @Description 确认完成
	 * @author luoteng
	 * @date 2018年6月14日:下午5:54:53
	 * @param proHandInDto
	 * @return HandResp
	 */
	@PostMapping("complete")
	public HandResp complete(@RequestBody ProHandInDto proHandInDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_HANDIN_PREFIX + proHandInDto.getLoginName();
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = new TransResult();
			try{
				result = proHandInService.handInComplete(proHandInDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}
		
		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
}
